package com.syh.jn.scm.domain.supplier.entity.base.goods;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsRepository;
import com.syh.jn.scm.dto.supplier.base.goods.goodsBrand.GoodsBrandMto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsBrand.GoodsBrandSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 商品品牌
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class GoodsBrand extends AbstractSimpleAggregate<Long> {
  /**
   * 品牌名称
   */
  private String name;
  /**
   * 编码
   */
  private String code;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 状态(1:可用 -1:不可用)
   */
  private Short status;
  /**
   * 排序等级
   */
  private Integer orderLevel;

  public GoodsBrand() {
  }

  public GoodsBrand(GoodsBrandSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    this.createTimeStamp = System.currentTimeMillis();
    this.name = sto.getName();
    this.businessEntityId = sto.getBusinessEntityId();
//    this.code ="234";//todo 缺少生成code的策略
    if (sto.getOrderLevel() == null) {
      this.orderLevel = 20;
    } else {
      this.orderLevel = sto.getOrderLevel();
    }

  }

  /**
   * 修改
   *
   * @param mto
   */
  public void modify(GoodsBrandMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this, false);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 数据销毁
   */
  public void destroy() {
    //启用状态不允许删除
    if (this.status.equals(Short.valueOf("1"))) {
      throw new BusinessException("品牌已启用,删除失败!");
    }
    GoodsRepository goodsQuery = (GoodsRepository) SpringContextUtils.getBean(GoodsRepository.class);
    if (goodsQuery.goodsExistsByColumn("goodsBrandId", id)) {
      throw new BusinessException("该生产商已被商品引用，删除失败！");
    }

  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = 1;
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = -1;
  }
}
